import { SecurityCollateral } from '@/project/entity/securityCollateral.entity';
import { Stage } from '@/project/entity/stage.entity';
import { Column, CreateDateColumn, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance, UpdateDateColumn, } from 'typeorm';
import { AccountType } from './accountType.entity';
import { Exclude, Expose } from 'class-transformer';
import { StageFundingFlow } from '@/project/entity/stageFundingFlow.entity';
import { InvestOrder } from '@/project/entity/investOrder.entity';
import { InvestFunding } from '@/project/entity/investFunding.entity';
import { decimalTransformer } from '@/project/entity/utils';
import Decimal from 'decimal.js';

export enum AccountRole {
  ADMIN = 'admin',
  INDIVIDUAL = 'individual',
  COMPNAY = 'company',
  TRUSTEE ='trustee'
}

@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export abstract class Account {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 32})
  role: AccountRole

  @Column({ length: 256, unique: true })
  name: string;

  @Column({ length: 256 })
  @Exclude()
  password: string;

  @Column({ type: 'decimal', precision: 20, scale: 4, nullable:true, transformer: decimalTransformer})
  balance?: Decimal;

  @Column({ length: 256,nullable: true })
  bankAccountName?: string;

  @Column({ length: 256,nullable: true })
  bankAccountNum?: string;

  @Column({ length: 256,nullable: true })
  bankBSBNumber?: string;

  @Column({ length: 2048,nullable: true })
  note?: string;

  @OneToMany(()=>AccountType, (ar) => ar.account)
  types: AccountType[]

  @Column({ default: false })
  isOffice?: boolean;

  @Column({ default: false})
  isTaxResidency?: boolean;

  // @OneToMany(()=>SecurityCollateral, (sc) => sc.debtor)
  // securityCollaterals: SecurityCollateral[];

  @OneToMany(() => StageFundingFlow, (f) => f.account)
  settlmentFlows: StageFundingFlow[]

  @ManyToMany(type=>Stage, (s)=> s.sponsors)
  sponsorStages: Stage[]

  @ManyToMany(type=>Stage, (s)=> s.lenders)
  lenderStages: Stage[]

  @ManyToMany(type=>Stage, (s)=> s.brokers)
  brokerStages: Stage[]

  @ManyToMany(type=>Stage, (s)=> s.lawyers)
  lawyerStages: Stage[]

  @ManyToMany(type=>Stage, (s)=> s.favoritedBy)
  favoriteStages: Stage[]

  // @OneToMany(type => Stage, (o) => o.managementFeeCollector)
  // manageStages: Stage[]

  @OneToMany(type => InvestOrder, (o) => o.investor)
  investments: InvestOrder[]

  @OneToMany(type => InvestFunding, (o) => o.investor)
  investFundings: InvestFunding[]

  @Column({ default: true })
  canLogin: boolean;

  @Column({ nullable: true })
  lastLoginAt?: Date;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  toJSON() {
    const { password, ...rest } = this;
    return rest;
  }
}
